var express = require('express');
var router = express.Router();

const { createPost, getPost, getPostAll, getPostPaginated, updatePost, deletePost } = require('../controllers/posts');

/**
 * @swagger
 * tags:
 *   name: Posts
 *   description: 帖子管理接口
 */
/**
 * @swagger
 * components:
 *   schemas:
 *     Post:
 *       type: object
 *       properties:
 *         post_id:
 *           type: integer
 *           readOnly: true
 *           example: 1
 *         author_id:
 *           type: integer
 *           description: 自动从登录用户获取
 *         title:
 *           type: string
 *           minLength: 3
 *           maxLength: 100
 *           example: "测试标题"
 *         content:    # 根据图片中的JSON类型字段
 *           type: object
 *           example: 
 *             {"blocks": [{"data": "测试文本", "type": "text", "format": "markdown"}, {"url": "http://localhost:3000/images/1200.png", "type": "image", "width": 1200, "height": 800}], "version": "1.0"}
 *         status:
 *           type: string
 *           enum: [draft, published]
 *           default: published
 *         view_count:
 *           type: integer
 *           readOnly: true
 *           example: 0
 *         like_count:
 *           type: integer
 *           readOnly: true
 *           example: 0
 *         created_at:
 *           type: string
 *           format: date-time
 *           readOnly: true
 *     PostDetail:
 *       allOf:
 *         - $ref: '#/components/schemas/Post'
 *         - type: object
 *           properties:
 *             username:
 *               type: string
 */

/**
 * @swagger
 * /posts:
 *   post:
 *     tags: [Posts]
 *     summary: 创建新帖子（支持草稿/发布状态）
 *     security:  # 新增安全认证要求
 *       - BearerAuth: []
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - title
 *               - content
 *             properties:
 *               title:
 *                 type: string
 *                 minLength: 3
 *                 maxLength: 100
 *                 example: "接口测试标题"
 *               content:
 *                 type: object
 *                 example: {"blocks": [{"data": "接口测试文本", "type": "text", "format": "markdown"}, {"url": "http://localhost:3000/images/1200.png", "type": "image", "width": 1200, "height": 800}], "version": "1.0"}
 *               status:
 *                 type: string
 *                 enum: [draft, published]
 *                 default: published
 *     responses:
 *       201:
 *         description: 创建成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/PostDetail'
 */
router.post('/', createPost);
/**
 * @swagger
 * /posts/getPostPaginated:
 *   get:
 *     tags: [Posts]
 *     summary: 分页获取帖子列表
 *     parameters:
 *       - in: query
 *         name: cursor
 *         schema:
 *           type: integer
 *         description: 游标ID（用于分页）
 *       - in: query
 *         name: pageSize
 *         schema:
 *           type: integer
 *           default: 10
 *         description: 每页数量
 *     responses:
 *       200:
 *         description: 成功获取
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 items:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/PostDetail'
 *                 next_cursor:
 *                   type: integer
 *                   example: 100
 *                 has_more:
 *                   type: boolean
 *                   example: true
 */
router.get('/getPostPaginated', getPostPaginated);
/**
 * @swagger
 * /posts/{id}:
 *   get:
 *     tags: [Posts]
 *     summary: 获取单个帖子详情
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功获取
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/PostDetail'
 *       404:
 *         description: 帖子未找到
 */
router.get('/:id', getPost);
/**
 * @swagger
 * /posts:
 *   get:
 *     tags: [Posts]
 *     summary: 获取所有帖子（非分页）
 *     responses:
 *       200:
 *         description: 成功获取
 *         content:
 *           application/json:
 *             schema:
 *               type: array
 *               items:
 *                 $ref: '#/components/schemas/Post'
 */

router.get('/', getPostAll);
/**
 * @swagger
 * /posts/{id}:
 *   put:
 *     tags: [Posts]
 *     summary: 更新帖子内容
 *     security:
 *       - BearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         example: 1
 *         description: 帖子ID
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               title:
 *                 type: string
 *                 minLength: 3
 *                 maxLength: 100
 *                 example: "更新测试标题"
 *               content:
 *                 type: object
 *                 example: 
 *                   {"blocks": [{"data": "更新测试文本", "type": "text", "format": "markdown"}, {"url": "http://localhost:3000/images/1200.png", "type": "image", "width": 1200, "height": 800}], "version": "1.0"}
 *     responses:
 *       200:
 *         description: 更新成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/PostDetail'
 *       400:
 *         description: 无效参数
 *       403:
 *         description: 无操作权限
 *       404:
 *         description: 帖子不存在
 */
router.put('/:id', updatePost);
/**
 * @swagger
 * /posts/{id}:
 *   delete:
 *     tags: [Posts]
 *     summary: 删除帖子
 *     security:
 *       - BearerAuth: []
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *         example: 1
 *         description: 帖子ID
 *     responses:
 *       200:
 *         description: 删除成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: "Post deleted"
 *       403:
 *         description: 无操作权限
 *       404:
 *         description: 帖子不存在
 */
router.delete('/:id', deletePost);

module.exports = router;
